package main

import (
	"errors"

	"fmt"
	"time"
	"yingsheng.com/go-api-server"
	"yingsheng.com/go-api-server/orm"
)

type SChain struct {
	ID         int64 `xorm:"pk autoincr" json:"id"`
	ChainID    string
	AppKey     string
	ActionName string
	Parameters string
	Response   string
	ErrorInfo  string
}

func (*SChain) DatabaseAlias() string {
	return "sso"
}

// 数据库表名
func (*SChain) TableName() string {
	return "APIChainLog"
}

var TestEngineer *orm.Engine
var err error
var da *orm.DA

// 初始化DA对象
func InitDA(da interface{}, model interface{}) error {
	mi, ok := model.(orm.ModelInterface)
	if !ok {
		return errors.New("model没有实现ModelInterface")
	}

	engine, err := TestEngineer.Get(mi)
	if err != nil {
		fmt.Println("error:", err.Error())
		return err
	}

	di, ok2 := da.(orm.DAInterface)
	if !ok2 {
		return errors.New("da没有实现DAInterface")
	}

	di.Init(engine, model)
	return nil
}

func main() {
	//初始化数据库连接池
	TestEngineer, err = orm.NewEngine()

	if err != nil {
		fmt.Println("An error occurs")
		apiserver.Error("An error occurs")
		return
	}
	md := &SChain{}
	da = &orm.DA{}
	InitDA(da, md)

	Init()

	for {
		time.Sleep(time.Second * 2)
	}
}
